#ifndef LEVEL4_CG_CGINC
#define LEVEL4_CG_CGINC
#include "UnityCG.cginc"

///	计算 切线空间
float3x3 Level4_TangentSpace(float3 normal,float4 tangent){
	float3x3 modelViewRot = float3x3(UNITY_MATRIX_MV);
	
	float3 inBinormal = cross(normal,tangent.xyz) * tangent.w;
	float3 t = mul(modelViewRot,tangent.xyz);
	float3 binormal = mul(modelViewRot,inBinormal.xyz);
	float3 n = mul(modelViewRot,normal);
	return float3x3(t,binormal,n);
}

//简单轮廓光
half4 _RimColor;
half _RimPower;
half _Atten;
inline fixed4 LightingHalfLambertRim(SurfaceOutput s,half3 lightDir,half3 viewDir,half atten){
	half3 halfVector = normalize(lightDir+viewDir);
	
	fixed NdotL = max(0,dot(s.Normal,lightDir));
	
	fixed NdotE = max(0,dot(s.Normal,viewDir));
	fixed NdotH = max(0,dot(s.Normal,halfVector));
	fixed EdotH = max(0,dot(viewDir,halfVector));
	
	fixed halfLambert = pow(NdotL * 0.5 + 0.5,2);
	fixed rimLight = 1 - NdotE;
	rimLight = pow(rimLight,_RimPower) * NdotH;
	
	fixed4 c;
	c.rgb = (s.Albedo * _LightColor0.rgb + rimLight.xxx * _RimColor) * (halfLambert * atten * _Atten);
	c.a = s.Alpha;
	return c;
}

#endif